<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class cart_mod extends module
{
    function __construct()
    {
        $this->uid = @$_SESSION['user']['uid'];
        parent::__construct();
    }
    function update_cart()
    {
        if (!empty($this->uid)) {
            $ucookie = $_COOKIE['goods']['cart'];
            $cquery = $this->db->get_all("select * from " . table('cart') . " where cookie = '" . $ucookie . "'");
            if ($cquery) {
                foreach ($cquery as $v) {
                    $crow = $this->db->get_one("select * from " . table('cart') . " where uid = " . $this->uid . " and sku_id = " . $v['sku_id'] . " limit 1");
                    if ($crow['id']) {
                        $this->db->delete(table("cart"), 'id = ' . $crow['id']);
                    }
                    $this->db->query("UPDATE " . table('cart') . " SET uid = '" . $this->uid . "', cookie = '' where id = " . $v['id']);
                }
            }
        }
    }
    function cart_all()
    {
        $this->update_cart();
        if (!empty($this->uid)) {
            $wsql = " where uid = " . $this->uid;
        } else {
            $ucookie = $_COOKIE['goods']['cart'];
            $wsql = " where cookie = '" . $ucookie . "'";
        }
        $cquery = $this->db->get_all("select * from " . table('cart') . $wsql . " order by id desc");
        if ($cquery) {
            foreach ($cquery as $v) {
                $sku_id_arr[] = $v['sku_id'];
                $gid_arr[] = $v['gid'];
            }
            if ($gid_arr) {
                $gid_in = implode(',', $gid_arr);
                $gquery = $this->db->get_all("select id,shop_id,title,price,images,market_price,stock from " . table('goods') . " where id in (" . $gid_in . ")");
                if ($gquery) {
                    foreach ($gquery as $v) {
                        $goods_id[$v['id']] = $v;
                        $shop_arr[] = $v['shop_id'];
                    }
                }
            }
            $sku_id_in = implode(',', $sku_id_arr);
            $sku_all = $this->db->get_all("select * from " . table('goods_sku_index') . " where id in (" . $sku_id_in . ")");
            if ($sku_all) {
                foreach ($sku_all as $v) {
                    $gidArr[] = $v['gid'];
                }
                $sku_img = $this->module('goods_sku')->get_sku_img($gidArr);
                $sec_price_arr = $this->module('goods_act')->seckill_price($gidArr);
                foreach ($sku_all as $v) {
                    $shop_id = $goods_id[$v['gid']]['shop_id'];
                    $v['shop_id'] = $shop_id;
                    $v['title'] = $goods_id[$v['gid']]['title'];
                    $v['market_price'] = $goods_id[$v['gid']]['market_price'];
                    $v['gid'] = $goods_id[$v['gid']]['id'];
                    if (!empty($v['value'])) {
                        $val_arr = explode(',', $v['value']);
                        foreach ($val_arr as $vv) {
                            if (!empty($sku_img[$v['gid']][$vv]['images'])) {
                                $v['images'] = $sku_img[$v['gid']][$vv]['images'];
                                break;
                            }
                        }
                    }
                    if (empty($v['images'])) {
                        $v['images'] = $goods_id[$v['gid']]['images'];
                    }
                    $v['sku_id'] = $v['id'];
                    $v['sku_name'] = unserialize($v['name']);
                    if (isset($post_cart_id[$v['id']])) {
                        $v['cartid'] = $post_cart_id[$v['id']];
                    }
                    if (isset($sec_price_arr[$v['id']])) {
                        $v['price'] = $sec_price_arr[$v['id']];
                    } else {
                        $v['price'] = $v['price'];
                    }
                    $v['stock'] = $v['stock'];
                    $goods_sku[$v['id']] = $v;
                }
            }
            foreach ($cquery as $v) {
                $cinfo['cartid'] = $v['id'];
                $cinfo['gid'] = $v['gid'];
                $cinfo['buy_num'] = $v['buy_num'];
                if (isset($goods_sku[$v['sku_id']])) {
                    $cinfo['goods_status'] = '1';
                    $cinfo['sku_id'] = $v['sku_id'];
                    $cinfo['shop_id'] = $shop_id;
                    $cinfo['title'] = $goods_sku[$v['sku_id']]['title'];
                    $cinfo['images'] = $goods_sku[$v['sku_id']]['images'];
                    $cinfo['price'] = $goods_sku[$v['sku_id']]['price'];
                    $cinfo['market_price'] = $goods_sku[$v['sku_id']]['market_price'];
                    $cinfo['stock'] = $goods_sku[$v['sku_id']]['stock'];
                    $cinfo['sku_name'] = $goods_sku[$v['sku_id']]['sku_name'];
                } else {
                    $cinfo['goods_status'] = '0';
                    $cinfo['title'] = $goods_id[$v['gid']]['title'];
                    $cinfo['images'] = $goods_id[$v['gid']]['images'];
                }
                $goods[$v['shop_id']][] = $cinfo;
                unset($cinfo);
            }
            $shop_data = $this->module('shop')->get_shop($shop_arr);
            $total_price = '0';
            foreach ($shop_data as $v) {
                $shop_total_price = '0';
                foreach ($goods[$v['id']] as $kk => $vv) {
                    if ($vv['stock'] - $buy_num >= 0) {
                        $vv['stock_status'] = '1';
                        $goods_total_price = $vv['price'] * $buy_num;
                        $shop_total_price += $goods_total_price;
                    }
                    $vv['goods_total_price'] = $goods_total_price;
                    $goods[$v['id']][$kk] = $vv;
                }
                $shop_pay[$v['id']]['sp_goods_price'] = $shop_total_price;
                $reduce_row = $this->module('goods_act')->reduce($v['id'], $shop_total_price);
                if ($reduce_row) {
                    $shop_pay[$v['id']]['sp_reduce_name'] = $reduce_row['name'];
                    $shop_pay[$v['id']]['sp_reduce_price'] = $reduce_row['reduce_price'];
                    $shop_total_price = $shop_total_price - $reduce_row['reduce_price'];
                }
                $shop_pay[$v['id']]['sp_total_price'] = $shop_total_price;
                $total_price += $shop_total_price;
            }
            foreach ($shop_data as $v) {
                $v['goods_price'] = $shop_pay[$v['id']]['sp_goods_price'];
                $v['total_price'] = $shop_pay[$v['id']]['sp_total_price'];
                if (isset($shop_pay[$v['id']]['sp_reduce_name'])) {
                    $v['reduce_name'] = $shop_pay[$v['id']]['sp_reduce_name'];
                    $v['reduce_price'] = $shop_pay[$v['id']]['sp_reduce_price'];
                }
                $v['goods'] = $goods[$v['id']];
                $goods_list[] = $v;
            }
            $result_data['list'] = $goods_list;
            $result_data['total_price'] = $total_price;
            return $result_data;
        }
    }
    function settle($arr = array())
    {
        $post_method = isset($_POST['post_method']) ? $_POST['post_method'] : '';
        if (!empty($post_method)) {
            if ($post_method == 'direct' || $post_method == 'group') {
                unset($_SESSION['post_order']);
                unset($_SESSION['post_method']);
                unset($_SESSION['post_share_id']);
                $post_sku[0] = array('sku_id' => isset($_POST['sku_id']) ? $_POST['sku_id'] : error_json('提交错误'), 'num' => isset($_POST['buy_num']) ? $_POST['buy_num'] : '1');
                $post_share_id = isset($_POST['share_id']) ? $_POST['share_id'] : '';
                $_SESSION['post_method'] = $post_method;
                $_SESSION['post_order'] = $post_sku;
                $_SESSION['post_share_id'] = $post_share_id;
            } else {
                return false;
            }
        } else {
            if (isset($_POST['cartid'])) {
                $cart_arr = isset($_POST['cartid']) ? $_POST['cartid'] : error_json('提交的订单为空');
                if (isset($cart_arr)) {
                    unset($_SESSION['post_order']);
                    unset($_SESSION['post_method']);
                    unset($_SESSION['post_share_id']);
                    @($_SESSION['post_method'] = '');
                    @($_SESSION['post_share_id'] = '');
                    $cart_in = implode(',', $cart_arr);
                    $cquery = $this->db->get_all("select * from " . table('cart') . " where id in(" . $cart_in . ")");
                    if ($cquery) {
                        foreach ($cquery as $v) {
                            $post_sku[] = array('sku_id' => $v['sku_id'], 'num' => $v['buy_num']);
                        }
                        $_SESSION['post_method'] = '';
                        $_SESSION['post_order'] = $post_sku;
                        $_SESSION['post_share_id'] = '';
                    }
                }
            } else {
                $post_method = $_SESSION['post_method'];
                $post_sku = $_SESSION['post_order'];
                $post_share_id = $_SESSION['post_share_id'];
            }
        }
        foreach ($post_sku as $v) {
            $sku_arr[] = $v['sku_id'];
            $post_buy_num[$v['sku_id']] = $v['num'];
            if (isset($v['cart_id'])) {
                $post_cart_id[$v['sku_id']] = $v['cart_id'];
            }
        }
        if (isset($sku_arr)) {
            $sku_id = implode(',', $sku_arr);
            $sku_all = $this->db->get_all("select * from " . table('goods_sku_index') . " where id in (" . $sku_id . ")");
            if ($sku_all) {
                foreach ($sku_all as $v) {
                    $gidArr[] = $v['gid'];
                }
                $gid_in = implode(',', $gidArr);
                $gquery = $this->db->get_all("select id,shop_id,title,price,images,market_price,stock,express from " . table('goods') . " where id in (" . $gid_in . ")");
                if ($gquery) {
                    foreach ($gquery as $v) {
                        $goods_id[$v['id']] = $v;
                        $shop_arr[] = $v['shop_id'];
                        $kd_arr[] = $v['express'];
                    }
                }
                $sku_img = $this->module('goods_sku')->get_sku_img($gidArr);
                if ($post_method == 'group') {
                    $pro_price_arr = $this->module('goods_act')->group_price($gidArr);
                } else {
                    $pro_price_arr = $this->module('goods_act')->seckill_price($gidArr);
                }
                foreach ($sku_all as $v) {
                    $shop_id = $goods_id[$v['gid']]['shop_id'];
                    $v['gid'] = $goods_id[$v['gid']]['id'];
                    $v['shop_id'] = $shop_id;
                    $v['title'] = $goods_id[$v['gid']]['title'];
                    $v['market_price'] = $goods_id[$v['gid']]['market_price'];
                    if (!empty($v['value'])) {
                        $val_arr = explode(',', $v['value']);
                        foreach ($val_arr as $vv) {
                            if (!empty($sku_img[$v['gid']][$vv]['images'])) {
                                $v['images'] = $sku_img[$v['gid']][$vv]['images'];
                                break;
                            }
                        }
                    }
                    if (empty($v['images'])) {
                        $v['images'] = $goods_id[$v['gid']]['images'];
                    }
                    $v['sku_id'] = $v['id'];
                    $v['sku_name'] = unserialize($v['name']);
                    if (isset($post_cart_id[$v['id']])) {
                        $v['cartid'] = $post_cart_id[$v['id']];
                    }
                    if (isset($pro_price_arr[$v['id']])) {
                        $v['price'] = $pro_price_arr[$v['id']];
                    } else {
                        $v['price'] = $v['price'];
                    }
                    $v['stock'] = $v['stock'];
                    $goods[$shop_id][] = $v;
                }
                $shop_data = $this->module('shop')->get_shop($shop_arr);
                $address_id = isset($arr['address_id']) ? $arr['address_id'] : '0';
                $express_arr = $this->module('express')->get_price($kd_arr, $address_id);
                $total_price = '0';
                foreach ($shop_data as $v) {
                    $shop_total_price = '0';
                    foreach ($goods[$v['id']] as $kk => $vv) {
                        $buy_num = $post_buy_num[$vv['sku_id']]['num'];
                        if ($vv['stock'] - $buy_num >= 0) {
                            $vv['stock_status'] = '1';
                            $goods_total_price = $vv['price'] * $buy_num;
                            $shop_total_price += $goods_total_price;
                        } else {
                            $vv['stock_status'] = '0';
                        }
                        $vv['goods_status'] = '1';
                        $vv['buy_num'] = $buy_num;
                        $vv['goods_total_price'] = $goods_total_price;
                        $goods[$v['id']][$kk] = $vv;
                    }
                    $shop_pay[$v['id']]['sp_goods_price'] = $shop_total_price;
                    $reduce_row = $this->module('goods_act')->reduce($v['id'], $shop_total_price);
                    if ($reduce_row) {
                        $shop_pay[$v['id']]['sp_reduce_name'] = $reduce_row['name'];
                        $shop_pay[$v['id']]['sp_reduce_price'] = $reduce_row['reduce_price'];
                        $shop_total_price = $shop_total_price - $reduce_row['reduce_price'];
                    }
                    $express_price = isset($express_arr[$v['id']]) ? $express_arr[$v['id']] : '0';
                    $shop_pay[$v['id']]['sp_express_price'] = $express_price;
                    $shop_total_price = $shop_total_price + $express_price;
                    $shop_pay[$v['id']]['sp_total_price'] = $shop_total_price;
                    $total_price += $shop_total_price;
                }
                foreach ($shop_data as $v) {
                    $v['goods_price'] = $shop_pay[$v['id']]['sp_goods_price'];
                    $v['express_price'] = $shop_pay[$v['id']]['sp_express_price'];
                    $v['total_price'] = $shop_pay[$v['id']]['sp_total_price'];
                    if (isset($shop_pay[$v['id']]['sp_reduce_name'])) {
                        $v['reduce_name'] = $shop_pay[$v['id']]['sp_reduce_name'];
                        $v['reduce_price'] = $shop_pay[$v['id']]['sp_reduce_price'];
                    }
                    $v['goods'] = $goods[$v['id']];
                    $goods_list[] = $v;
                }
                $result_data['list'] = $goods_list;
                $result_data['total_price'] = $total_price;
                if (!empty($post_method)) {
                    $result_data['post_method'] = $post_method;
                    $result_data['share_id'] = $post_share_id;
                    $result_data['post_sku_id'] = $post_sku[0]['sku_id'];
                    $result_data['post_buy_num'] = $post_sku[0]['num'];
                }
                return $result_data;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    function create_order($settle_info, $param)
    {
        $time = time();
        $trade_no = build_order_no();
        if (!empty($settle_info['list'])) {
            foreach ($settle_info['list'] as $v) {
                $this->db->autocommit();
                $order_no = build_order_no();
                $oarr['uid'] = !empty($this->uid) ? $this->uid : '0';
                $oarr['shop_id'] = $v['id'];
                $oarr['order_no'] = $order_no;
                if (!empty($settle_info['post_method'])) {
                    if ($settle_info['post_method'] == 'group') {
                        $oarr['otype'] = '2';
                        $oarr['share_id'] = $settle_info['share_id'];
                        $oarr['share_gid'] = $settle_info['list'][0]['goods'][0]['gid'];
                    }
                } else {
                    $oarr['otype'] = '0';
                }
                $oarr['total_price'] = $v['total_price'];
                $oarr['goods_price'] = $v['goods_price'];
                $oarr['express_price'] = isset($v['express_price']) ? $v['express_price'] : '0';
                $oarr['message'] = !empty($param['message'][$v['id']]) ? trim($param['message'][$v['id']]) : '';
                $oarr['position'] = _browser_pid();
                $oarr['order_status'] = '1';
                $oarr['addtime'] = $time;
                $order_query = $this->db->add(table('order_info'), $oarr);
                $oid = $this->db->insert_id();
                $express_row = $this->db->get_one("select * from " . table('member_address') . " where id = " . $param['address_id']);
                $addrarr['oid'] = $oid;
                $addrarr['name'] = $express_row['name'];
                $addrarr['prov'] = $express_row['prov'];
                $addrarr['prov_cn'] = $express_row['prov_cn'];
                $addrarr['city'] = $express_row['city'];
                $addrarr['city_cn'] = $express_row['city_cn'];
                $addrarr['dist'] = $express_row['dist'];
                $addrarr['dist_cn'] = $express_row['dist_cn'];
                $addrarr['address'] = $express_row['address'];
                $addrarr['mobile'] = $express_row['mobile'];
                $addrarr['zip'] = $express_row['zip'];
                $addrarr['kd_price'] = isset($v['express_price']) ? $v['express_price'] : '0';
                $express_query = $this->db->add(table('order_address'), $addrarr);
                $setpaysql['uid'] = !empty($this->uid) ? $this->uid : '0';
                $setpaysql['pay_type'] = '1';
                $setpaysql['settle_type'] = '1';
                $setpaysql['trade_no'] = $trade_no;
                $setpaysql['order_no'] = $order_no;
                $setpaysql['pay_price'] = $v['total_price'];
                $setpaysql['pay_status'] = '0';
                $setpaysql['ip'] = get_ip();
                $setpaysql['addtime'] = $time;
                $admin_pay_query = $this->db->add(table('admin_pay'), $setpaysql);
                foreach ($v['goods'] as $kk => $vv) {
                    $ogarr['oid'] = $oid;
                    $ogarr['uid'] = !empty($this->uid) ? $this->uid : '0';
                    $ogarr['shop_id'] = $vv['shop_id'];
                    $ogarr['gid'] = $vv['gid'];
                    $ogarr['title'] = $vv['title'];
                    $ogarr['images'] = $vv['images'];
                    $ogarr['price'] = $vv['price'];
                    $ogarr['market_price'] = $vv['market_price'];
                    $ogarr['sku_id'] = $vv['sku_id'];
                    $ogarr['sku_value'] = isset($vv['sku_name']) ? serialize($vv['sku_name']) : '';
                    $ogarr['barcode'] = isset($vv['barcode']) ? $vv['barcode'] : '';
                    $ogarr['buy_num'] = $vv['buy_num'];
                    $ogarr['service_id'] = $order_no . $kk;
                    $ogarr['order_status'] = '1';
                    $ogarr['status'] = '1';
                    $ogarr['addtime'] = $time;
                    $q1 = $this->db->add(table('order_goods'), $ogarr);
                    $q2 = $this->db->query("update " . table('goods') . " SET `stock` = `stock`- " . $vv['buy_num'] . " WHERE `id` = " . $vv['id'] . " LIMIT 1;");
                    $q3 = $this->db->query("update " . table('goods_sku_index') . " SET `stock` = `stock`- " . $vv['buy_num'] . " WHERE `id` = " . $vv['sku_id'] . " LIMIT 1;");
                    if ($q1 && $q2 && $q3) {
                        $goods_count[] = '';
                    }
                }
                if ($order_query && $express_query && $admin_pay_query && count($v['goods']) == count($goods_count) && count($goods_count) != '0') {
                    $this->db->commit();
                } else {
                    $this->db->rollback();
                }
                unset($goods_count);
            }
            $post_method = isset($_POST['post_method']) ? $_POST['post_method'] : '';
            if (!empty($post_method)) {
                $result['trade_no'] = $trade_no;
                $result['total_price'] = $settle_info['total_price'];
                return $result;
            } else {
                $destroy_post = $this->destroy_post();
                if ($destroy_post) {
                    $result['trade_no'] = $trade_no;
                    $result['total_price'] = $settle_info['total_price'];
                    return $result;
                } else {
                    return false;
                }
            }
        } else {
            return false;
        }
    }
    function destroy_post()
    {
        $this->db->autocommit();
        $post_order = @$_SESSION['post_order'];
        foreach ($post_order as $v) {
            $cart_sku_arr[] = $v['sku_id'];
        }
        $cart_sku_in = implode(',', $cart_sku_arr);
        if (!empty($this->uid)) {
            $wsql = " uid = " . $this->uid;
        } else {
            $cookie = @$_COOKIE['goods']['cart'];
            $wsql = " cookie = '" . $cookie . "' ";
        }
        $query = $this->db->delete(table('cart'), $wsql . " and id in (" . $cart_sku_in . ")");
        if ($query) {
            unset($_SESSION['post_order']);
            $this->db->commit();
            return true;
        } else {
            $this->db->rollback();
            return false;
        }
    }
}